﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.GeometriesGraph
{
	public class QuadrantOp
	{
		private QuadrantOp()
		{
		}

		public static int Quadrant(double dx, double dy)
		{
			if (dx == 0.0 && dy == 0.0)
			{
				throw new ArgumentException(string.Concat(new object[]
				{
					"Cannot compute the quadrant for point ( ",
					dx,
					", ",
					dy,
					" )"
				}));
			}
			if (dx >= 0.0)
			{
				if (dy >= 0.0)
				{
					return QuadrantOp.NE;
				}
				return QuadrantOp.SE;
			}
			else
			{
				if (dy >= 0.0)
				{
					return QuadrantOp.NW;
				}
				return QuadrantOp.SW;
			}
		}

		public static int Quadrant(Coordinate p0, Coordinate p1)
		{
			if (p1.X == p0.X && p1.Y == p0.Y)
			{
				throw new ArgumentException("Cannot compute the quadrant for two identical points " + p0);
			}
			if (p1.X >= p0.X)
			{
				if (p1.Y >= p0.Y)
				{
					return QuadrantOp.NE;
				}
				return QuadrantOp.SE;
			}
			else
			{
				if (p1.Y >= p0.Y)
				{
					return QuadrantOp.NW;
				}
				return QuadrantOp.SW;
			}
		}

		public static bool IsOpposite(int quad1, int quad2)
		{
			return quad1 != quad2 && (quad1 - quad2 + 4) % 4 == 2;
		}

		public static int CommonHalfPlane(int quad1, int quad2)
		{
			if (quad1 == quad2)
			{
				return quad1;
			}
			if ((quad1 - quad2 + 4) % 4 == 2)
			{
				return -1;
			}
			int num = (quad1 < quad2) ? quad1 : quad2;
			int num2 = (quad1 > quad2) ? quad1 : quad2;
			if (num == 0 && num2 == 3)
			{
				return 3;
			}
			return num;
		}

		public static bool IsInHalfPlane(int quad, int halfPlane)
		{
			if (halfPlane == QuadrantOp.SE)
			{
				return quad == QuadrantOp.SE || quad == QuadrantOp.SW;
			}
			return quad == halfPlane || quad == halfPlane + 1;
		}

		public static bool IsNorthern(int quad)
		{
			return quad == QuadrantOp.NE || quad == QuadrantOp.NW;
		}

		public static readonly int NE = 0;

		public static readonly int NW = 1;

		public static readonly int SW = 2;

		public static readonly int SE = 3;
	}
}
